package com.jianneng.jdg.model.homeactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.com.jianneng.jdg.R;
import com.jianneng.jdg.model.fragment.FragmentCircle;
import com.jianneng.jdg.model.fragment.FragmentEntrance;
import com.jianneng.jdg.model.fragment.FragmentMyNews;
import com.jianneng.jdg.model.fragment.FragmentMyWork;
import com.jianneng.jdg.model.fragment.FragmentSchool;

public class MainActivity extends FragmentActivity implements OnClickListener {

	private RelativeLayout rl_entrance, rl_mywork, rl_circle, rl_school,
			rl_mynews;
	private Fragment mFragmentEntrance, mFragmentMyWork, mFragmentCircle,
			mFragmentSchool, mFragmentMyNews, currentFragment;
	private ImageView iv_entrance, iv_mywork, iv_circle, iv_school, iv_mynews;
	private TextView tv_entrance, tv_mywork, tv_circle, tv_school, tv_mynews;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		setContentView(R.layout.activity_mian);
		initUI();
		/**
		 * 切换fragment
		 **/
		initTab();
		getExtra();

	}

	private void getExtra() {
		Intent intent = getIntent();
		String str = intent.getStringExtra("id");
		if ("4".equals(str)){
			clickMyNews();
		}
		if("3".equals(str)){
			clickCircle();
		}
		if("2".equals(str)){
			clickCircle();
			iv_entrance.setImageResource(R.drawable.home);
			tv_entrance.setTextColor(getResources().getColor(
					R.color.bottomtab_normal));
		}
		String map = intent.getStringExtra("map");
		if("1".equals(map)){
			clickMyWork();
			Bundle bundle = new Bundle();
			mFragmentMyWork.setArguments(bundle);
		}
		
		String stringExtra = intent.getStringExtra("success");
		if("true".equals(stringExtra)){
			Bundle bundle = new Bundle();  
			bundle.putString("key", stringExtra);  
			mFragmentMyNews.setArguments(bundle);
		}	
		
	}

	private void initUI() {

		rl_entrance = (RelativeLayout) findViewById(R.id.rl_entrance);
		rl_mywork = (RelativeLayout) findViewById(R.id.rl_mywork);
		rl_circle = (RelativeLayout) findViewById(R.id.rl_circle);
		rl_school = (RelativeLayout) findViewById(R.id.rl_school);
		rl_mynews = (RelativeLayout) findViewById(R.id.rl_mynews);

		rl_entrance.setOnClickListener(this);
		rl_mywork.setOnClickListener(this);
		rl_circle.setOnClickListener(this);
		rl_school.setOnClickListener(this);
		rl_mynews.setOnClickListener(this);

		iv_entrance = (ImageView) findViewById(R.id.iv_entrance);
		iv_mywork = (ImageView) findViewById(R.id.iv_mywork);
		iv_circle = (ImageView) findViewById(R.id.iv_circle);
		iv_school = (ImageView) findViewById(R.id.iv_school);
		iv_mynews = (ImageView) findViewById(R.id.iv_mynews);

		tv_entrance = (TextView) findViewById(R.id.tv_entrance);
		tv_mywork = (TextView) findViewById(R.id.tv_mywork);
		tv_circle = (TextView) findViewById(R.id.tv_circle);
		tv_school = (TextView) findViewById(R.id.tv_school);
		tv_mynews = (TextView) findViewById(R.id.tv_mynews);

	}

	private void initTab() {

		if (mFragmentEntrance == null) {
			mFragmentEntrance = new FragmentEntrance();
		}

		if (!mFragmentEntrance.isAdded()) {
			// �ύ����
			getSupportFragmentManager().beginTransaction()
					.add(R.id.rl_fragment, mFragmentEntrance).commit();
			// ��¼��ǰFragment
			currentFragment = mFragmentEntrance;
			// 设置底部当前颜色
			iv_entrance.setImageResource(R.drawable.home_s);
			tv_entrance.setTextColor(getResources().getColor(
					R.color.bottomtab_press));
			iv_mywork.setImageResource(R.drawable.home);
			tv_mywork.setTextColor(getResources().getColor(
					R.color.bottomtab_normal));
			iv_school.setImageResource(R.drawable.school);
			tv_school.setTextColor(getResources().getColor(
					R.color.bottomtab_normal));
			iv_mynews.setImageResource(R.drawable.user);
			tv_mynews.setTextColor(getResources().getColor(
					R.color.bottomtab_normal));
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_entrance:
			clickEntrance();//快捷入口
			break;
		case R.id.rl_mywork://我的工作
			clickMyWork();
			break;
		case R.id.rl_circle://圈圈
			clickCircle();
			break;
		case R.id.rl_school://打工学校
			clickSchool();
			break;
		case R.id.rl_mynews://个人信息
			clickMyNews();
			break;
		default:
			break;
		}

	}

	private void clickEntrance() {
		if (mFragmentEntrance == null) {
			mFragmentEntrance = new FragmentEntrance();
		}
		showOtherFragment(getSupportFragmentManager().beginTransaction(),
				mFragmentEntrance);

		iv_entrance.setImageResource(R.drawable.home_s);
		tv_entrance.setTextColor(getResources().getColor(
				R.color.bottomtab_press));
		iv_mywork.setImageResource(R.drawable.home);
		tv_mywork.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
		iv_school.setImageResource(R.drawable.school);
		tv_school.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
		iv_mynews.setImageResource(R.drawable.user);
		tv_mynews.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
	}

	private void clickMyWork() {

		if (mFragmentMyWork == null) {
			mFragmentMyWork = new FragmentMyWork();
		}
		showOtherFragment(getSupportFragmentManager().beginTransaction(),
				mFragmentMyWork);

		iv_entrance.setImageResource(R.drawable.home);
		tv_entrance.setTextColor(getResources().getColor(
				R.color.bottomtab_normal));
		iv_mywork.setImageResource(R.drawable.home_s);
		tv_mywork
				.setTextColor(getResources().getColor(R.color.bottomtab_press));
		iv_school.setImageResource(R.drawable.school);
		tv_school.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
		iv_mynews.setImageResource(R.drawable.user);
		tv_mynews.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
	}

	private void clickCircle() {
		if (mFragmentCircle == null) {
			mFragmentCircle = new FragmentCircle();
		}
		showOtherFragment(getSupportFragmentManager().beginTransaction(),
				mFragmentCircle);

	}

	private void clickSchool() {
		if (mFragmentSchool == null) {
			mFragmentSchool = new FragmentSchool();
		}
		showOtherFragment(getSupportFragmentManager().beginTransaction(),
				mFragmentSchool);
		iv_entrance.setImageResource(R.drawable.home);
		tv_entrance.setTextColor(getResources().getColor(
				R.color.bottomtab_normal));
		iv_mywork.setImageResource(R.drawable.home);
		tv_mywork.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
		iv_school.setImageResource(R.drawable.school_s);
		tv_school
				.setTextColor(getResources().getColor(R.color.bottomtab_press));
		iv_mynews.setImageResource(R.drawable.user);
		tv_mynews.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
	}

	private void clickMyNews() {
		if (mFragmentMyNews == null) {

			mFragmentMyNews = new FragmentMyNews();
		}
		showOtherFragment(getSupportFragmentManager().beginTransaction(),
				mFragmentMyNews);
		iv_entrance.setImageResource(R.drawable.home);
		tv_entrance.setTextColor(getResources().getColor(
				R.color.bottomtab_normal));
		iv_mywork.setImageResource(R.drawable.home);
		tv_mywork.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
		iv_school.setImageResource(R.drawable.school);
		tv_school.setTextColor(getResources()
				.getColor(R.color.bottomtab_normal));
		iv_mynews.setImageResource(R.drawable.user_s);
		tv_mynews
				.setTextColor(getResources().getColor(R.color.bottomtab_press));
	}

	private void showOtherFragment(FragmentTransaction transaction,
			Fragment fragment) {
		if (currentFragment == fragment) {
			return;
		}
		if (!fragment.isAdded()) { 
			transaction.hide(currentFragment).add(R.id.rl_fragment, fragment)
					.commit();
		} else {
			transaction.hide(currentFragment).show(fragment).commit();
		}
		currentFragment = fragment;
	}

}
